home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectInput / ActionBasic / Chart.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  8.9 KB  |  251 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Chart.cs
  3. //
  4. // Desc: Chart control
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. using System;
  9. using System.Collections;
  10. using System.Drawing;
  11. using System.Drawing.Drawing2D;
  12. using System.Windows.Forms;
  13.  
  14.  
  15. namespace ActionBasic 
  16. {
  17.     /// <summary>
  18.     /// Summary description for Chart.
  19.     /// </summary>
  20.     public class Chart : System.Windows.Forms.Panel
  21.     {
  22.         private const int GutterSize     = 10;   // Chart display constants
  23.         private const int CellSize       = 15;
  24.         
  25.         // Stored data to be drawn
  26.         private String[] ActionNames = null;
  27.         private ArrayList DeviceStates = null;
  28.  
  29.         private bool RefreshNeeded = true;
  30.         
  31.         private Rectangle ChartRect = new Rectangle(0, 0, 0, 0);  // Bounding RECT for the chart (grid and titles)
  32.         private Rectangle GridRect  = new Rectangle(0, 0, 0, 0);  // Bounding RECT for the grid
  33.        
  34.         Brush   ActiveBrush   = new SolidBrush( Color.FromKnownColor( KnownColor.Highlight ) );
  35.         Brush   UnmappedBrush = new HatchBrush( HatchStyle.BackwardDiagonal, Color.FromKnownColor( KnownColor.Highlight ), Color.FromKnownColor( KnownColor.Control ) );
  36.         Brush   InactiveBrush = new SolidBrush( Color.FromKnownColor( KnownColor.Control ) );
  37.         Pen     GridPen       = new Pen( Color.FromKnownColor( KnownColor.ControlDark ) );
  38.         Pen     AxisPen       = new Pen( Color.FromKnownColor( KnownColor.Control ) );
  39.         
  40.         private Font ActionFont = new Font("arial", 9, FontStyle.Regular, GraphicsUnit.Pixel, 0, true );
  41.         private Font DeviceFont = new Font("arial", 9, FontStyle.Regular, GraphicsUnit.Pixel, 0, false );
  42.  
  43.         public Chart()
  44.         {
  45.             SetStyle(ControlStyles.UserPaint, true);
  46.             SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  47.             SetStyle(ControlStyles.DoubleBuffer, true);
  48.         }
  49.  
  50.         public String[] ColumnTitles
  51.         {
  52.             get
  53.             {
  54.                 return ActionNames;
  55.             }
  56.             set
  57.             {
  58.                 ActionNames = value;
  59.                 RefreshNeeded = true;
  60.                 Invalidate();
  61.             }
  62.         }
  63.  
  64.         public ArrayList RowData
  65.         {
  66.             get
  67.             {
  68.                 return DeviceStates;
  69.             }
  70.             set
  71.             {
  72.                 DeviceStates = value;
  73.                 RefreshNeeded = true;
  74.                 Invalidate();
  75.             }
  76.         }
  77.  
  78.         
  79.  
  80.         public void UpdateData()
  81.         {
  82.             int iY = GridRect.Top;
  83.     
  84.             foreach( DeviceState state in DeviceStates )
  85.             { 
  86.                 Rectangle rc = new Rectangle( GridRect.Left + 3, iY + 3, 10, 10 ); 
  87.  
  88.                 for( int i=0; i < state.InputState.Length; i++ )
  89.                 {
  90.                     if( state.InputState[i] != state.PaintState[i] )
  91.                         Invalidate( rc );
  92.  
  93.                     rc.Offset( CellSize, 0 );
  94.                 }
  95.                 iY += CellSize; 
  96.             }
  97.             Invalidate(false);            
  98.         }
  99.  
  100.         protected void PaintChart( Graphics g )
  101.         {
  102.             int iY = GridRect.Top;
  103.             
  104.             foreach( DeviceState state in DeviceStates )
  105.             { 
  106.                 Rectangle rc = new Rectangle( GridRect.Left + 3, iY + 3, 10, 10 ); 
  107.  
  108.                 for( int i=0; i < state.InputState.Length; i++ )
  109.                 {
  110.                     if( state.IsMapped[i] == false )
  111.                     {
  112.                         g.FillRectangle( UnmappedBrush, rc );
  113.                     }
  114.                     else if( state.InputState[i] != 0 )
  115.                     {
  116.                         g.FillRectangle( ActiveBrush, rc );
  117.  
  118.                         if( (GameActions)i == GameActions.Walk )
  119.                         {
  120.                             // Scale the axis data to the size of the cell 
  121.                             int tempX = rc.Left+1 + (state.InputState[i] + 100)/25; 
  122.  
  123.                             g.DrawLine( AxisPen, new Point( tempX,   rc.Bottom-2 ), new Point( tempX,   rc.Top ) );
  124.                             g.DrawLine( AxisPen, new Point( tempX-1, rc.Top+2 ),    new Point( tempX+2, rc.Top+2 ) ); 
  125.                             g.DrawLine( AxisPen, new Point( tempX-2, rc.Top+3 ),    new Point( tempX+3, rc.Top+3 ) );
  126.                         }
  127.                     }
  128.                     else
  129.                     {
  130.                         g.FillRectangle( InactiveBrush, rc );
  131.                     }
  132.  
  133.                     state.PaintState[i] = state.InputState[i];       
  134.                     rc.Offset( CellSize, 0 );
  135.                 }
  136.  
  137.                 iY += CellSize; 
  138.             }
  139.         }
  140.  
  141.         protected void PaintLegend( Graphics g )
  142.         {
  143.             // Paint the legend
  144.             g.DrawString( "Inactive", DeviceFont, Brushes.Black, 25, 8 );
  145.             g.DrawRectangle( GridPen, 8, 8, 12, 12 );
  146.  
  147.             g.DrawString( "Active", DeviceFont, Brushes.Black, 25, 24 );
  148.             g.DrawRectangle( GridPen, 8, 24, 12, 12 );
  149.             g.FillRectangle( ActiveBrush, 10, 26, 9, 9 );
  150.  
  151.             g.DrawString( "Unmapped", DeviceFont, Brushes.Black, 25, 40 );
  152.             g.DrawRectangle( GridPen, 8, 40, 12, 12 );
  153.             g.FillRectangle( UnmappedBrush, 10, 42, 9, 9 );
  154.         }
  155.  
  156.         protected void PaintGrid( Graphics g )
  157.         {
  158.             int iX = GridRect.Left;
  159.             int iY = GridRect.Top - GutterSize;
  160.  
  161.             StringFormat formatAction = new StringFormat( StringFormatFlags.DirectionVertical );
  162.             formatAction.Alignment = StringAlignment.Far;
  163.             foreach( String action in ActionNames )
  164.             {
  165.                 g.DrawString( action, ActionFont, Brushes.Black, iX+2, iY, formatAction );
  166.                 g.DrawRectangle( Pens.Gray, iX, GridRect.Top, CellSize, GridRect.Bottom - GridRect.Top );
  167.  
  168.                 iX += CellSize;
  169.             }
  170.     
  171.             iY = GridRect.Top;
  172.  
  173.             foreach( DeviceState state in DeviceStates )
  174.             {
  175.                 g.DrawString( state.Name, DeviceFont, Brushes.Black, GridRect.Left - GutterSize, iY+1, new StringFormat( StringFormatFlags.DirectionRightToLeft ) );
  176.                 g.DrawRectangle( Pens.Gray, GridRect.Left, iY, GridRect.Right - GridRect.Left, CellSize );
  177.             
  178.                 iY += CellSize; 
  179.             }
  180.         }
  181.  
  182.         /// <summary>
  183.         /// Override of the paint method. All of the code in this method is
  184.         /// devoted to correctly positioning the chart and drawing labels.
  185.         /// </summary>
  186.         /// <seealso>PaintChart</seealso>
  187.         /// <param name="e">Paint Argumenets</param>
  188.         protected override void OnPaint( PaintEventArgs e )
  189.         {
  190.             base.OnPaint( e );
  191.  
  192.             Graphics g = e.Graphics;
  193.  
  194.             if( null == ActionNames || 
  195.                 null == DeviceStates )
  196.             {
  197.                 return;
  198.             }
  199.             
  200.             try
  201.             {
  202.                 if( RefreshNeeded )
  203.                     RefreshChart( g );
  204.  
  205.                 PaintLegend( g );   
  206.                 PaintGrid( g );
  207.                 PaintChart( g );
  208.             }
  209.             catch(System.InvalidOperationException)
  210.             {
  211.                 OnPaint(e);
  212.                 Application.DoEvents();
  213.             }
  214.         }
  215.  
  216.         private void RefreshChart( Graphics g )
  217.         {
  218.             RefreshNeeded = false;
  219.  
  220.             int iMaxActionSize=0, iMaxDeviceSize=0;   
  221.  
  222.             // Determine the largest action name size
  223.             foreach( string name in ActionNames )
  224.             {
  225.                 Size size = ( g.MeasureString( name, ActionFont ) ).ToSize();
  226.                 iMaxActionSize = Math.Max( iMaxActionSize, size.Width );
  227.             }
  228.  
  229.             // Determine the largest device name size
  230.             foreach( DeviceState state in DeviceStates )
  231.             {
  232.                 Size size = ( g.MeasureString( state.Name, DeviceFont ) ).ToSize();
  233.                 iMaxDeviceSize = Math.Max( iMaxDeviceSize, size.Width );
  234.             }
  235.  
  236.             // Determine the bounding rectangle for the chart and grid
  237.             GridRect.Width  = CellSize * ActionNames.Length;
  238.             GridRect.Height = CellSize * DeviceStates.Count;
  239.  
  240.             ChartRect.Width = GridRect.Width + iMaxDeviceSize + GutterSize;
  241.             ChartRect.Height = GridRect.Height + iMaxActionSize + GutterSize;
  242.  
  243.             ChartRect.X      = ( this.Width  - ChartRect.Width  ) / 2;
  244.             ChartRect.Y      = ( this.Height - ChartRect.Height ) / 2;
  245.  
  246.             GridRect.X = ChartRect.X + iMaxDeviceSize + GutterSize;
  247.             GridRect.Y = ChartRect.Y + iMaxActionSize + GutterSize;
  248.         }
  249.     }
  250. }
  251.